package com.grass.finding.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grass.finding.model.domain.Lost;
import com.grass.finding.model.dto.LostDto;
import com.grass.finding.service.LostService;
import com.grass.finding.mapper.LostMapper;
import io.swagger.models.auth.In;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.util.*;

/**
* @author 20158
* @description 针对表【lost(被寻找人的信息)】的数据库操作Service实现
* @createDate 2023-02-21 22:35:30
*/
@Service
public class LostServiceImpl extends ServiceImpl<LostMapper, Lost>
    implements LostService {

    @Autowired
    LostMapper lostMapper;

    @Autowired
    ModelMapper modelMapper;

    @Override
    public List<LostDto> getPublishByOpenId(String openId) {
        QueryWrapper<Lost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openId", openId);
//        List<Lost> lostList = lostMapper.selectList(queryWrapper);
        List<Lost> losts = lostMapper.selectPublishByOpenId(openId);

        ModelMapper modelMapper = new ModelMapper();
        List<LostDto> lostDtos = modelMapper.map(losts, new TypeToken<List<LostDto>>(){}.getType());

        return addIndexFiled(lostDtos);
//        return null;
    }

    @Override
    public LostDto getPublishByLostId(Integer lostId) {
        Lost lost = lostMapper.selectPublishByLostId(lostId);
        return modelMapper.map(lost, LostDto.class);
    }

    @Override
    public List<LostDto> searchByName(String name) {

        StringBuffer stringBuffer = new StringBuffer();
        Map<Lost, Integer> map = new HashMap<>();
        int num = 0;
        for (int i = 0; i < name.length(); i++){
            if(name.charAt(i) == ' ' || i == name.length() - 1){
                if(i == name.length() - 1) stringBuffer.append(name.charAt(i));
                if(stringBuffer.length() == 0) continue;
                num++;
                QueryWrapper<Lost> queryWrapper = new QueryWrapper<>();
                queryWrapper.like("name", stringBuffer.toString())
                        .or().like("lostAddress", stringBuffer.toString())
                        .or().like("description", stringBuffer.toString())
                        .or().like("origin", stringBuffer.toString())
                        .or().like("other", stringBuffer.toString());
                stringBuffer.delete(0, stringBuffer.length());
                List<Lost> losts = lostMapper.selectList(queryWrapper);
                for (int k = 0; k < losts.size(); k++){
                    map.put(losts.get(k), map.getOrDefault(losts.get(k), 0) + 1);
                }
            }

            if(name.charAt(i) != ' ') stringBuffer.append(name.charAt(i));
        }


        List<Lost> lostList = new ArrayList<>();
        for(Map.Entry<Lost, Integer>entry : map.entrySet()){
            if(entry.getValue() == num) lostList.add(entry.getKey());
        }
        return addIndexFiled(modelMapper.map(lostList, new TypeToken<List<LostDto>>(){}.getType()));
    }

    @Override
    public boolean deleteByLostId(Integer lostId) {
        long delete = lostMapper.deleteByLostId(lostId);
        return delete >= 1;
    }

    @Override
    public List<LostDto> getPublish() {
        List<Lost> losts = lostMapper.getPublish();
        return addIndexFiled(modelMapper.map(losts, new TypeToken<List<LostDto>>(){}.getType()));
    }

    /**
     * 添加 index 变量，前端瀑布流展示
     * @param lostDtoList
     * @return
     */
    private List<LostDto> addIndexFiled(List<LostDto> lostDtoList){
        for (int i = 0; i < lostDtoList.size(); i++) lostDtoList.get(i).setIndex(i + 1);
        return lostDtoList;
    }
}




